package post_pb

import (
	context "context"
	fmt "fmt"
	gorm1 "github.com/infobloxopen/atlas-app-toolkit/gorm"
	query "github.com/infobloxopen/atlas-app-toolkit/query"
	errors "github.com/infobloxopen/protoc-gen-gorm/errors"
	gorm "github.com/jinzhu/gorm"
	field_mask "google.golang.org/genproto/protobuf/field_mask"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
	strings "strings"
	time "time"
)

type PostORM struct {
	CommentCount uint64
	Content      string
	CreatedAt    *time.Time
	Id           uint64
	Title        string
	UpdatedAt    *time.Time
	UserId       uint64
	Uuid         string
}

// TableName overrides the default tablename generated by GORM
func (PostORM) TableName() string {
	return "posts"
}

// ToORM runs the BeforeToORM hook if present, converts the fields of this
// object to ORM format, runs the AfterToORM hook, then returns the ORM object
func (m *Post) ToORM(ctx context.Context) (PostORM, error) {
	to := PostORM{}
	var err error
	if prehook, ok := interface{}(m).(PostWithBeforeToORM); ok {
		if err = prehook.BeforeToORM(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		t := m.CreatedAt.AsTime()
		to.CreatedAt = &t
	}
	if m.UpdatedAt != nil {
		t := m.UpdatedAt.AsTime()
		to.UpdatedAt = &t
	}
	to.Title = m.Title
	to.Content = m.Content
	to.UserId = m.UserId
	to.Uuid = m.Uuid
	to.CommentCount = m.CommentCount
	if posthook, ok := interface{}(m).(PostWithAfterToORM); ok {
		err = posthook.AfterToORM(ctx, &to)
	}
	return to, err
}

// ToPB runs the BeforeToPB hook if present, converts the fields of this
// object to PB format, runs the AfterToPB hook, then returns the PB object
func (m *PostORM) ToPB(ctx context.Context) (Post, error) {
	to := Post{}
	var err error
	if prehook, ok := interface{}(m).(PostWithBeforeToPB); ok {
		if err = prehook.BeforeToPB(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		to.CreatedAt = timestamppb.New(*m.CreatedAt)
	}
	if m.UpdatedAt != nil {
		to.UpdatedAt = timestamppb.New(*m.UpdatedAt)
	}
	to.Title = m.Title
	to.Content = m.Content
	to.UserId = m.UserId
	to.Uuid = m.Uuid
	to.CommentCount = m.CommentCount
	if posthook, ok := interface{}(m).(PostWithAfterToPB); ok {
		err = posthook.AfterToPB(ctx, &to)
	}
	return to, err
}

// The following are interfaces you can implement for special behavior during ORM/PB conversions
// of type Post the arg will be the target, the caller the one being converted from

// PostBeforeToORM called before default ToORM code
type PostWithBeforeToORM interface {
	BeforeToORM(context.Context, *PostORM) error
}

// PostAfterToORM called after default ToORM code
type PostWithAfterToORM interface {
	AfterToORM(context.Context, *PostORM) error
}

// PostBeforeToPB called before default ToPB code
type PostWithBeforeToPB interface {
	BeforeToPB(context.Context, *Post) error
}

// PostAfterToPB called after default ToPB code
type PostWithAfterToPB interface {
	AfterToPB(context.Context, *Post) error
}

// DefaultCreatePost executes a basic gorm create call
func DefaultCreatePost(ctx context.Context, in *Post, db *gorm.DB) (*Post, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeCreate_); ok {
		if db, err = hook.BeforeCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Create(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithAfterCreate_); ok {
		if err = hook.AfterCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	return &pbResponse, err
}

type PostORMWithBeforeCreate_ interface {
	BeforeCreate_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type PostORMWithAfterCreate_ interface {
	AfterCreate_(context.Context, *gorm.DB) error
}

func DefaultReadPost(ctx context.Context, in *Post, db *gorm.DB, fs *query.FieldSelection) (*Post, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if ormObj.Id == 0 {
		return nil, errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeReadApplyQuery); ok {
		if db, err = hook.BeforeReadApplyQuery(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	if db, err = gorm1.ApplyFieldSelection(ctx, db, fs, &PostORM{}); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeReadFind); ok {
		if db, err = hook.BeforeReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	ormResponse := PostORM{}
	if err = db.Where(&ormObj).First(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormResponse).(PostORMWithAfterReadFind); ok {
		if err = hook.AfterReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormResponse.ToPB(ctx)
	return &pbResponse, err
}

type PostORMWithBeforeReadApplyQuery interface {
	BeforeReadApplyQuery(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type PostORMWithBeforeReadFind interface {
	BeforeReadFind(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type PostORMWithAfterReadFind interface {
	AfterReadFind(context.Context, *gorm.DB, *query.FieldSelection) error
}

func DefaultDeletePost(ctx context.Context, in *Post, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return err
	}
	if ormObj.Id == 0 {
		return errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeDelete_); ok {
		if db, err = hook.BeforeDelete_(ctx, db); err != nil {
			return err
		}
	}
	err = db.Where(&ormObj).Delete(&PostORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithAfterDelete_); ok {
		err = hook.AfterDelete_(ctx, db)
	}
	return err
}

type PostORMWithBeforeDelete_ interface {
	BeforeDelete_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type PostORMWithAfterDelete_ interface {
	AfterDelete_(context.Context, *gorm.DB) error
}

func DefaultDeletePostSet(ctx context.Context, in []*Post, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	var err error
	keys := []uint64{}
	for _, obj := range in {
		ormObj, err := obj.ToORM(ctx)
		if err != nil {
			return err
		}
		if ormObj.Id == 0 {
			return errors.EmptyIdError
		}
		keys = append(keys, ormObj.Id)
	}
	if hook, ok := (interface{}(&PostORM{})).(PostORMWithBeforeDeleteSet); ok {
		if db, err = hook.BeforeDeleteSet(ctx, in, db); err != nil {
			return err
		}
	}
	err = db.Where("id in (?)", keys).Delete(&PostORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := (interface{}(&PostORM{})).(PostORMWithAfterDeleteSet); ok {
		err = hook.AfterDeleteSet(ctx, in, db)
	}
	return err
}

type PostORMWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, []*Post, *gorm.DB) (*gorm.DB, error)
}
type PostORMWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, []*Post, *gorm.DB) error
}

// DefaultStrictUpdatePost clears / replaces / appends first level 1:many children and then executes a gorm update call
func DefaultStrictUpdatePost(ctx context.Context, in *Post, db *gorm.DB) (*Post, error) {
	if in == nil {
		return nil, fmt.Errorf("Nil argument to DefaultStrictUpdatePost")
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	lockedRow := &PostORM{}
	db.Model(&ormObj).Set("gorm:query_option", "FOR UPDATE").Where("id=?", ormObj.Id).First(lockedRow)
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeStrictUpdateCleanup); ok {
		if db, err = hook.BeforeStrictUpdateCleanup(ctx, db); err != nil {
			return nil, err
		}
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeStrictUpdateSave); ok {
		if db, err = hook.BeforeStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Save(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithAfterStrictUpdateSave); ok {
		if err = hook.AfterStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	if err != nil {
		return nil, err
	}
	return &pbResponse, err
}

type PostORMWithBeforeStrictUpdateCleanup interface {
	BeforeStrictUpdateCleanup(context.Context, *gorm.DB) (*gorm.DB, error)
}
type PostORMWithBeforeStrictUpdateSave interface {
	BeforeStrictUpdateSave(context.Context, *gorm.DB) (*gorm.DB, error)
}
type PostORMWithAfterStrictUpdateSave interface {
	AfterStrictUpdateSave(context.Context, *gorm.DB) error
}

// DefaultPatchPost executes a basic gorm update call with patch behavior
func DefaultPatchPost(ctx context.Context, in *Post, updateMask *field_mask.FieldMask, db *gorm.DB) (*Post, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	var pbObj Post
	var err error
	if hook, ok := interface{}(&pbObj).(PostWithBeforePatchRead); ok {
		if db, err = hook.BeforePatchRead(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbReadRes, err := DefaultReadPost(ctx, &Post{Id: in.GetId()}, db, nil)
	if err != nil {
		return nil, err
	}
	pbObj = *pbReadRes
	if hook, ok := interface{}(&pbObj).(PostWithBeforePatchApplyFieldMask); ok {
		if db, err = hook.BeforePatchApplyFieldMask(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	if _, err := DefaultApplyFieldMaskPost(ctx, &pbObj, in, updateMask, "", db); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&pbObj).(PostWithBeforePatchSave); ok {
		if db, err = hook.BeforePatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := DefaultStrictUpdatePost(ctx, &pbObj, db)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(pbResponse).(PostWithAfterPatchSave); ok {
		if err = hook.AfterPatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	return pbResponse, nil
}

type PostWithBeforePatchRead interface {
	BeforePatchRead(context.Context, *Post, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type PostWithBeforePatchApplyFieldMask interface {
	BeforePatchApplyFieldMask(context.Context, *Post, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type PostWithBeforePatchSave interface {
	BeforePatchSave(context.Context, *Post, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type PostWithAfterPatchSave interface {
	AfterPatchSave(context.Context, *Post, *field_mask.FieldMask, *gorm.DB) error
}

// DefaultPatchSetPost executes a bulk gorm update call with patch behavior
func DefaultPatchSetPost(ctx context.Context, objects []*Post, updateMasks []*field_mask.FieldMask, db *gorm.DB) ([]*Post, error) {
	if len(objects) != len(updateMasks) {
		return nil, fmt.Errorf(errors.BadRepeatedFieldMaskTpl, len(updateMasks), len(objects))
	}

	results := make([]*Post, 0, len(objects))
	for i, patcher := range objects {
		pbResponse, err := DefaultPatchPost(ctx, patcher, updateMasks[i], db)
		if err != nil {
			return nil, err
		}

		results = append(results, pbResponse)
	}

	return results, nil
}

// DefaultApplyFieldMaskPost patches an pbObject with patcher according to a field mask.
func DefaultApplyFieldMaskPost(ctx context.Context, patchee *Post, patcher *Post, updateMask *field_mask.FieldMask, prefix string, db *gorm.DB) (*Post, error) {
	if patcher == nil {
		return nil, nil
	} else if patchee == nil {
		return nil, errors.NilArgumentError
	}
	var err error
	var updatedCreatedAt bool
	var updatedUpdatedAt bool
	for i, f := range updateMask.Paths {
		if f == prefix+"Id" {
			patchee.Id = patcher.Id
			continue
		}
		if !updatedCreatedAt && strings.HasPrefix(f, prefix+"CreatedAt.") {
			if patcher.CreatedAt == nil {
				patchee.CreatedAt = nil
				continue
			}
			if patchee.CreatedAt == nil {
				patchee.CreatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"CreatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.CreatedAt, patchee.CreatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"CreatedAt" {
			updatedCreatedAt = true
			patchee.CreatedAt = patcher.CreatedAt
			continue
		}
		if !updatedUpdatedAt && strings.HasPrefix(f, prefix+"UpdatedAt.") {
			if patcher.UpdatedAt == nil {
				patchee.UpdatedAt = nil
				continue
			}
			if patchee.UpdatedAt == nil {
				patchee.UpdatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"UpdatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.UpdatedAt, patchee.UpdatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"UpdatedAt" {
			updatedUpdatedAt = true
			patchee.UpdatedAt = patcher.UpdatedAt
			continue
		}
		if f == prefix+"Title" {
			patchee.Title = patcher.Title
			continue
		}
		if f == prefix+"Content" {
			patchee.Content = patcher.Content
			continue
		}
		if f == prefix+"UserId" {
			patchee.UserId = patcher.UserId
			continue
		}
		if f == prefix+"Uuid" {
			patchee.Uuid = patcher.Uuid
			continue
		}
		if f == prefix+"CommentCount" {
			patchee.CommentCount = patcher.CommentCount
			continue
		}
	}
	if err != nil {
		return nil, err
	}
	return patchee, nil
}

// DefaultListPost executes a gorm list call
func DefaultListPost(ctx context.Context, db *gorm.DB, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) ([]*Post, error) {
	in := Post{}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeListApplyQuery); ok {
		if db, err = hook.BeforeListApplyQuery(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db, err = gorm1.ApplyCollectionOperators(ctx, db, &PostORM{}, &Post{}, f, s, p, fs)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithBeforeListFind); ok {
		if db, err = hook.BeforeListFind(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db = db.Where(&ormObj)
	db = db.Order("id")
	ormResponse := []PostORM{}
	if err := db.Find(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(PostORMWithAfterListFind); ok {
		if err = hook.AfterListFind(ctx, db, &ormResponse, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	pbResponse := []*Post{}
	for _, responseEntry := range ormResponse {
		temp, err := responseEntry.ToPB(ctx)
		if err != nil {
			return nil, err
		}
		pbResponse = append(pbResponse, &temp)
	}
	return pbResponse, nil
}

type PostORMWithBeforeListApplyQuery interface {
	BeforeListApplyQuery(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type PostORMWithBeforeListFind interface {
	BeforeListFind(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type PostORMWithAfterListFind interface {
	AfterListFind(context.Context, *gorm.DB, *[]PostORM, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) error
}
type PostServiceDefaultServer struct {
	DB *gorm.DB
}

// IncrementCommentCount ...
func (m *PostServiceDefaultServer) IncrementCommentCount(ctx context.Context, in *IncrementCommentCountRequest) (*IncrementCommentCountResponse, error) {
	out := &IncrementCommentCountResponse{}
	return out, nil
}

// IncrementCommentCountCompensate ...
func (m *PostServiceDefaultServer) IncrementCommentCountCompensate(ctx context.Context, in *IncrementCommentCountRequest) (*IncrementCommentCountResponse, error) {
	out := &IncrementCommentCountResponse{}
	return out, nil
}

// DecrementCommentCount ...
func (m *PostServiceDefaultServer) DecrementCommentCount(ctx context.Context, in *DecrementCommentCountRequest) (*DecrementCommentCountResponse, error) {
	out := &DecrementCommentCountResponse{}
	return out, nil
}

// DecrementCommentCountCompensate ...
func (m *PostServiceDefaultServer) DecrementCommentCountCompensate(ctx context.Context, in *DecrementCommentCountRequest) (*DecrementCommentCountResponse, error) {
	out := &DecrementCommentCountResponse{}
	return out, nil
}

// Create ...
func (m *PostServiceDefaultServer) Create(ctx context.Context, in *CreatePostRequest) (*CreatePostResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(PostServicePostWithBeforeCreate); ok {
		var err error
		if db, err = custom.BeforeCreate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultCreatePost(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &CreatePostResponse{Result: res}
	if custom, ok := interface{}(in).(PostServicePostWithAfterCreate); ok {
		var err error
		if err = custom.AfterCreate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// PostServicePostWithBeforeCreate called before DefaultCreatePost in the default Create handler
type PostServicePostWithBeforeCreate interface {
	BeforeCreate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// PostServicePostWithAfterCreate called before DefaultCreatePost in the default Create handler
type PostServicePostWithAfterCreate interface {
	AfterCreate(context.Context, *CreatePostResponse, *gorm.DB) error
}

// Read ...
func (m *PostServiceDefaultServer) Read(ctx context.Context, in *ReadPostRequest) (*ReadPostResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(PostServicePostWithBeforeRead); ok {
		var err error
		if db, err = custom.BeforeRead(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultReadPost(ctx, &Post{Id: in.GetId()}, db, in.Fields)
	if err != nil {
		return nil, err
	}
	out := &ReadPostResponse{Result: res}
	if custom, ok := interface{}(in).(PostServicePostWithAfterRead); ok {
		var err error
		if err = custom.AfterRead(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// PostServicePostWithBeforeRead called before DefaultReadPost in the default Read handler
type PostServicePostWithBeforeRead interface {
	BeforeRead(context.Context, *gorm.DB) (*gorm.DB, error)
}

// PostServicePostWithAfterRead called before DefaultReadPost in the default Read handler
type PostServicePostWithAfterRead interface {
	AfterRead(context.Context, *ReadPostResponse, *gorm.DB) error
}

// Update ...
func (m *PostServiceDefaultServer) Update(ctx context.Context, in *UpdatePostRequest) (*UpdatePostResponse, error) {
	var err error
	var res *Post
	db := m.DB
	if custom, ok := interface{}(in).(PostServicePostWithBeforeUpdate); ok {
		var err error
		if db, err = custom.BeforeUpdate(ctx, db); err != nil {
			return nil, err
		}
	}
	if in.GetGerogeriGegege() == nil {
		res, err = DefaultStrictUpdatePost(ctx, in.GetPayload(), db)
	} else {
		res, err = DefaultPatchPost(ctx, in.GetPayload(), in.GetGerogeriGegege(), db)
	}
	if err != nil {
		return nil, err
	}
	out := &UpdatePostResponse{Result: res}
	if custom, ok := interface{}(in).(PostServicePostWithAfterUpdate); ok {
		var err error
		if err = custom.AfterUpdate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// PostServicePostWithBeforeUpdate called before DefaultUpdatePost in the default Update handler
type PostServicePostWithBeforeUpdate interface {
	BeforeUpdate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// PostServicePostWithAfterUpdate called before DefaultUpdatePost in the default Update handler
type PostServicePostWithAfterUpdate interface {
	AfterUpdate(context.Context, *UpdatePostResponse, *gorm.DB) error
}

// UpdateSet ...
func (m *PostServiceDefaultServer) UpdateSet(ctx context.Context, in *UpdateSetPostRequest) (*UpdateSetPostResponse, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}

	db := m.DB

	if custom, ok := interface{}(in).(PostServicePostWithBeforeUpdateSet); ok {
		var err error
		if db, err = custom.BeforeUpdateSet(ctx, db); err != nil {
			return nil, err
		}
	}

	res, err := DefaultPatchSetPost(ctx, in.GetObjects(), in.GetMasks(), db)
	if err != nil {
		return nil, err
	}

	out := &UpdateSetPostResponse{Results: res}

	if custom, ok := interface{}(in).(PostServicePostWithAfterUpdateSet); ok {
		var err error
		if err = custom.AfterUpdateSet(ctx, out, db); err != nil {
			return nil, err
		}
	}

	return out, nil
}

// PostServicePostWithBeforeUpdateSet called before DefaultUpdateSetPost in the default UpdateSet handler
type PostServicePostWithBeforeUpdateSet interface {
	BeforeUpdateSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// PostServicePostWithAfterUpdateSet called before DefaultUpdateSetPost in the default UpdateSet handler
type PostServicePostWithAfterUpdateSet interface {
	AfterUpdateSet(context.Context, *UpdateSetPostResponse, *gorm.DB) error
}

// List ...
func (m *PostServiceDefaultServer) List(ctx context.Context, in *ListPostRequest) (*ListPostResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(PostServicePostWithBeforeList); ok {
		var err error
		if db, err = custom.BeforeList(ctx, db); err != nil {
			return nil, err
		}
	}
	pagedRequest := false
	if in.GetPaging().GetLimit() >= 1 {
		in.Paging.Limit++
		pagedRequest = true
	}
	res, err := DefaultListPost(ctx, db, in.Filter, in.OrderBy, in.Paging, in.Fields)
	if err != nil {
		return nil, err
	}
	var resPaging *query.PageInfo
	if pagedRequest {
		var offset int32
		var size int32 = int32(len(res))
		if size == in.GetPaging().GetLimit() {
			size--
			res = res[:size]
			offset = in.GetPaging().GetOffset() + size
		}
		resPaging = &query.PageInfo{Offset: offset}
	}
	out := &ListPostResponse{Results: res, PageInfo: resPaging}
	if custom, ok := interface{}(in).(PostServicePostWithAfterList); ok {
		var err error
		if err = custom.AfterList(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// PostServicePostWithBeforeList called before DefaultListPost in the default List handler
type PostServicePostWithBeforeList interface {
	BeforeList(context.Context, *gorm.DB) (*gorm.DB, error)
}

// PostServicePostWithAfterList called before DefaultListPost in the default List handler
type PostServicePostWithAfterList interface {
	AfterList(context.Context, *ListPostResponse, *gorm.DB) error
}

// Delete ...
func (m *PostServiceDefaultServer) Delete(ctx context.Context, in *DeletePostRequest) (*DeletePostResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(PostServicePostWithBeforeDelete); ok {
		var err error
		if db, err = custom.BeforeDelete(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeletePost(ctx, &Post{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	out := &DeletePostResponse{}
	if custom, ok := interface{}(in).(PostServicePostWithAfterDelete); ok {
		var err error
		if err = custom.AfterDelete(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// PostServicePostWithBeforeDelete called before DefaultDeletePost in the default Delete handler
type PostServicePostWithBeforeDelete interface {
	BeforeDelete(context.Context, *gorm.DB) (*gorm.DB, error)
}

// PostServicePostWithAfterDelete called before DefaultDeletePost in the default Delete handler
type PostServicePostWithAfterDelete interface {
	AfterDelete(context.Context, *DeletePostResponse, *gorm.DB) error
}

// DeleteSet ...
func (m *PostServiceDefaultServer) DeleteSet(ctx context.Context, in *DeletesPostRequest) (*DeletePostResponse, error) {
	db := m.DB
	objs := []*Post{}
	for _, id := range in.Ids {
		objs = append(objs, &Post{Id: id})
	}
	if custom, ok := interface{}(in).(PostServicePostWithBeforeDeleteSet); ok {
		var err error
		if db, err = custom.BeforeDeleteSet(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeletePostSet(ctx, objs, db)
	if err != nil {
		return nil, err
	}
	out := &DeletePostResponse{}
	if custom, ok := interface{}(in).(PostServicePostWithAfterDeleteSet); ok {
		var err error
		if err = custom.AfterDeleteSet(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// PostServicePostWithBeforeDeleteSet called before DefaultDeleteSetPost in the default DeleteSet handler
type PostServicePostWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// PostServicePostWithAfterDeleteSet called before DefaultDeleteSetPost in the default DeleteSet handler
type PostServicePostWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, *DeletePostResponse, *gorm.DB) error
}
